import { Provide } from '@midwayjs/core'
import { InjectEntityModel } from '@midwayjs/typegoose'
import { ReturnModelType } from '@typegoose/typegoose'
import { WorkflowTemplateOptions } from '../../interface.js'
import { WorkflowTemplate } from '../../entity/examples/workflowTemplate.js'

@Provide()
export class WorkflowTemplateService {
  @InjectEntityModel(WorkflowTemplate)
  workflowTemplateModel: ReturnModelType<typeof WorkflowTemplate>

  async insertMany(
    data: WorkflowTemplateOptions[]
  ): Promise<WorkflowTemplateOptions[]> {
    return this.workflowTemplateModel.insertMany(data)
  }

  async find(): Promise<any> {
    return this.workflowTemplateModel.find().exec()
  }

  async findOne(data) {
    return this.workflowTemplateModel.findOne(data).exec()
  }

  async updateOne(data): Promise<any> {
    const _id = data._id
    Reflect.deleteProperty(data, '_id')
    return this.workflowTemplateModel.updateOne({ _id }, { $set: data }).exec()
  }

  async deleteOne(data: any): Promise<any> {
    return await this.workflowTemplateModel.deleteOne(data).exec()
  }
}
